Ext.onReady(function() {
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [ {
			text : '角色管理',
			xtype : 'button',
			listeners:{
				click: function(){
					location.href = "/sysMgr/role_mgr"
				}
			}
		},'-',{
			text : '刷新',
			xtype : 'button',
			listeners:{
				click: function(){
					Ext.getCmp("user_grid").getStore().reload();
				}
			}
		}]
	});
	
	var store = Ext.create('Ext.data.Store', {
	    fields:['Id', 'Username', 'Name', {
	    	name: "Department", type: 'object'
	    },{
	    	name: "Sex", type: 'int'
	    },{
	    	name: "Position", type: 'object'
	    },{
	    	name: "Flight", type: "object"
	    },{
	    	name: "Education", type: 'object'
	    },/*"RFID",*/"DocumentNumber","TwoInchPhotos","Recruitment","HomeAddress",{
	    	name: "ToPartInTheWorkTime",  type: 'date'
	    }, "ContactNumber" ,{
	    	name: "Birthday", type: 'date'
	    },{
	    	name: "PoliticalStatus", type: "object"
	    },{
	    	name: "Code", type: "int"
	    },{
	    	name:"roles", type:"object"
	    }],
	    autoLoad:true,
	    pageSize: 25,
	    proxy: {
	        type: 'ajax',
	        url: '/user/all',
	        reader: {
	            type: 'json',
	            root: 'data.rows',
	            totalProperty: 'data.total'
	        }
	    }
	});
	
	var grid = Ext.create('Ext.grid.Panel', {
		id:"user_grid",
		store: store,
		margin: '5 5',
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '用户名',
			renderer: function(v, meta, record){
				return record.get('Username')+'('+record.get('Name')+')';
			}
		}, {
			text : '角色',
			flex:1,
			renderer: function(v, meta, record){
				var roles = record.get("roles");
				var names = '';
				if(roles.length > 0){
					for(var i=0; i<roles.length; i++){
						names += roles[i].Name;
						if(i!=roles.length-1){
							names +=',';
						}
					}
				}
				return names;
			}
		}],
		bbar : Ext.create('Ext.PagingToolbar', {
							store : store,
							border: false,
							displayInfo : true,
							displayMsg : '显示{0} - {1} / 总数 {2}',
							emptyMsg : "没数据"
						})
	});
	
	var panel = Ext.create("Ext.panel.Panel", {
		title: '',
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar ],
		items : [ grid ]
	});
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ]
	});
});